package org.xiaojun.loginDemo.controller;


import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.xiaojun.loginDemo.service.IUserService;

import javax.servlet.http.HttpSession;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xj
 * @since 2021-08-06
 */
@Controller
public class UserController {

    private static final Logger log = LoggerFactory.getLogger(UserController.class);

    @Autowired
    IUserService userService;

    @RequestMapping("/registPage")
    public String registPage(){
        return "regist";
    }

    @RequestMapping("/regist")
    public String regist(String userName, String password, String code, HttpSession session, Model model){

        String msg = userService.regist(userName, password, code, session);
        if (StringUtils.isBlank(msg)){
            model.addAttribute("msg", msg);
            return "regist";
        }
        return "redirect:/index";
    }

    @RequestMapping("/login")
    public String login(String userName, String password, String code, Model model, HttpSession session){

        String msg = userService.login(userName, password, code, session);
        if (StringUtils.isNotBlank(msg)){
            model.addAttribute("msg", msg);
            return "index";
        }

        return "redirect:/employee/list";
    }




}
